package com.aftersale.demo.utils;

import com.aftersale.demo.utils.WebUtils;
import com.aftersale.demo.entity.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
*@Author  空
*@Date    2023/10/23 20:25
*@Desc
*/
@Component
public class AuthenticationUtil implements AuthenticationEntryPoint {
    private static String ERROR="Full authentication is required to access this resource";
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String msg=authException.getMessage();
        if(ERROR.equals(msg)){
            msg="认证失败，请重新登录";
        }
        Result result = Result.error("401", msg);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);

    }
}